package nc.impl.hrpbm.fourcgxm.fourcgxm;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.bs.dao.BaseDAO;
import nc.bs.fileupload.itf.IBipFileUploadService;
import nc.bs.framework.common.NCLocator;
import nc.bs.util.DefdocUtils;
import nc.itf.hrpbm.fourcgxm.fourcgxm.ICGProjectToNCProjectService;
import nc.itf.hrpbm.fourcgxm.fourcgxm.IFourcgxmVOService;
import nc.itf.hrpbm.sjxm.sjxmh.ISjxmHVOService;
import nc.vo.fourcgxm.fourcgxm.AggFourcgxmVO;
import nc.vo.fourcgxm.fourcgxm.FourcgxmVO;
import nc.vo.hrpbm.budgetprocureproject.BugetProcureProjectHVO;
import nc.vo.pf.pub.util.ArrayUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.IVOMeta;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.sjxm.sjxm.AggSjxmVO;
import nc.vo.sjxm.sjxm.AggSjxmVOMeta;
import nc.vo.sjxm.sjxm.SjxmBVO;
import nc.vo.sjxm.sjxm.SjxmHVO;

public class CGProjectToNCProjectServiceImpl implements ICGProjectToNCProjectService {
	private IFourcgxmVOService fourcgxmVOService;
	private IBipFileUploadService bipFileUploadService;
	private ISjxmHVOService sjxmHVOService;
	private BaseDAO baseDAO;

	@Override
	public AggSjxmVO[] saveCGProjectToFourCGXMVOs(List<BugetProcureProjectHVO> lists) throws BusinessException {
		AggSjxmVO[] aggvos = CGProjectToFourCGXMVOs(lists);
		try {
			aggvos = this.getSjxmHVOService().saveAggSjxmVO(aggvos);
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		return aggvos;
	}
	

	@Override
	public List<BugetProcureProjectHVO> writeCGProject(AggSjxmVO[] aggvos , List<BugetProcureProjectHVO> lists) throws BusinessException {
		if(ArrayUtil.isNull(aggvos)) return lists;
		for(AggSjxmVO aggvo : aggvos ) {
			SjxmHVO hvo = (SjxmHVO)aggvo.getParent();
			Object attach = hvo.getAttributeValue("attach");
			Object pk_cgid = hvo.getAttributeValue("pk_cgid");
			for(BugetProcureProjectHVO cgvo : lists) {
				String cgid = cgvo.getPk_cgid();
				if(pk_cgid.equals(cgid)) {
					cgvo.setHopbillcode(hvo.getHopbillcode());
					break;
				}
			}
			if(attach != null ) {
				List<String> fileURLs = new ArrayList<>();
				JSONArray attachs = JSON.parseArray(attach.toString());
				for(Object obj : attachs) {
					JSONObject att = JSON.parseObject(obj.toString());
					fileURLs.add(att.toString());
				}
				if(!fileURLs.isEmpty() ) {
					this.getBipFileUploadService().fileUploadNC(hvo.getPk_sjxm(), hvo.getPk_sjxm(),
							hvo.getCgbillcode(), fileURLs, hvo.getPk_group(), hvo.getPk_org(), hvo.getPk_org_v(), "CGTOHOP", hvo.getCreator());
				
				}
			}
		}
		return lists;
	}
	
	/**
	 * 将采购系统vo转成NC四级项目
	 * @return
	 * @throws BusinessException
	 */
	protected AggSjxmVO[] CGProjectToFourCGXMVOs(List<BugetProcureProjectHVO> lists) throws BusinessException {
		List<AggSjxmVO> arrs = new ArrayList<AggSjxmVO>();
		if(ArrayUtil.isNull(lists)) return null;
		List<Map<String, Object>> listmaps = DefdocUtils.getDefdoc("CGHRP_CG002");
		if(listmaps == null) return null;
		for(BugetProcureProjectHVO cgvo : lists) {
			AggSjxmVO aggvo = new AggSjxmVO();
			SjxmBVO bvo = new SjxmBVO();
			bvo.setStatus(VOStatus.NEW);
			SjxmHVO parvo = new SjxmHVO();
			parvo.setStatus(VOStatus.NEW);
			for(Map<String, Object> map : listmaps) {
				String att = map.get("code") == null ? "" : map.get("code").toString();
				String attvale = map.get("shortname") == null ? "": map.get("shortname").toString();
				Object value = "".equals(attvale) ? "" : cgvo.getAttributeValue(attvale);
				parvo.setAttributeValue(att, value);
			}
			bvo.setAttributeValue("pk_org",parvo.getAttributeValue("pk_org"));
			bvo.setAttributeValue("pk_org_v",parvo.getAttributeValue("pk_org_v"));
			bvo.setAttributeValue("budstate","2");
			bvo.setAttributeValue("fourstate","1");
			String applyyear = cgvo.getApplyyear();
			bvo.setAttributeValue("ysyear",applyyear);
			bvo.setAttributeValue("modifyobj","CG");
			bvo.setAttributeValue("bdef1","N");
			bvo.setAttributeValue("modifyobj","CG");
			SjxmBVO[] sjxmBVOS = this.handlerBVOs(bvo, applyyear);
			aggvo.setChildrenVO(sjxmBVOS);
			parvo.setCreator("1001A2100000000282KF");
			parvo.setBillmaker("1001A2100000000282KF");
			parvo.setMaketime(new UFDateTime());
			parvo.setCreationtime(new UFDateTime());
			parvo.setBilltype("CGTOHOP");
			parvo.setApprovestatus(-1);
			aggvo.setParent(parvo);
			parvo.setStatus(2);
			arrs.add(aggvo);
		}
		return arrs.toArray(new AggSjxmVO[arrs.size()]);
	}

	/**
	 * 默认当前三年使用
	 * @param bvo
	 * @param ysyear
	 * @return
	 * @throws BusinessException
	 */
	protected SjxmBVO[] handlerBVOs(SjxmBVO bvo ,String ysyear) throws  BusinessException{
		int i = 6;
		Integer nd = Integer.valueOf(ysyear).intValue() - 1;
		SjxmBVO[] bvos = new SjxmBVO[i];
		for (int j = 0 ; j < i; j++){
			nd = nd + j;
			SjxmBVO clvo = (SjxmBVO)bvo.clone();
			clvo.setAttributeValue("ysyear", String.valueOf(nd));
			bvos[j] = clvo;
		}
		return bvos;
	}
	
	public BaseDAO getBaseDAO() {
		return baseDAO == null ? new BaseDAO() : baseDAO;
	}

	public IFourcgxmVOService getFourcgxmVOService() {
		if(null == fourcgxmVOService) {
			fourcgxmVOService = NCLocator.getInstance().lookup(IFourcgxmVOService.class);
		}
		return fourcgxmVOService;
	}


	public IBipFileUploadService getBipFileUploadService() {
		if(null == bipFileUploadService) {
			bipFileUploadService = NCLocator.getInstance().lookup(IBipFileUploadService.class);
		}
		return bipFileUploadService;
	}

	public ISjxmHVOService getSjxmHVOService() {
		if(null == this.sjxmHVOService){
			this.sjxmHVOService = NCLocator.getInstance().lookup(ISjxmHVOService.class);
		}
		return this.sjxmHVOService;
	}
}
